const moment = require('moment')
const md5 = require('md5')
class Classes {
  constructor() { }
  // 班级展示
  async list(req, res) {
    let { pagesize, currentpage } = req.query
    pagesize = pagesize || 2
    currentpage = currentpage || 1
    let start = (currentpage - 1) * pagesize
    let total = await req.db.query('select count(1) count from `classes`', { type: req.db.QueryTypes.SELECT })
    let sql = 'select * from `classes` limit ' + start + ',' + pagesize
    let result = await req.db.query(sql,{type: req.db.QueryTypes.SELECT})
    res.send({
      error:0,
      message:'success',
      total: total[0].count,
      data:result
    })
  }

  // 班级全部展示
  async listAll(req, res) {
    let sql = 'select * from `classes` '
    let result = await req.db.query(sql,{type: req.db.QueryTypes.SELECT})
    res.send({
      error:0,
      message:'success',
      data:result
    })
  }

  // 班级添加 begin
  add(req, res) {
    let { classname, status } = req.body
    let ctime = moment().format('YYYY-MM-DD HH:m:s')
    let sql = 'insert into `classes` (`classname`,`status`,`ctime`) values ("' + classname + '","' + status + '","' + ctime + '")'
    req.db.query(sql).then(result => {
      res.send({
        info: req.session,
        error: 0,
        message: 'success'
      })
    }).catch(error => {
      console.log(error)
      res.send({
        error: 10,
        message: 'insert fail'
      })
    })
  }
  // 班级添加 end

  // 班级是否存在
  isexist(req,res) {
    let { fieldname, fieldvalue } = req.query
    let sql = 'select count(1) count from `classes` where `' + fieldname + '`="' + fieldvalue + '"'
    req.db.query(sql).spread((result) => {
      if (result[0].count === 0) {
        res.send({
          error: 0,
          message: '不存在'
        })
      } else {
        res.send({
          error: 1,
          message: '存在'
        })
      }
    })
  }
  usernameExist(req, res) {
    let username = req.body.username
    if(req.body.oldname){
      let oldname=req.body.oldname
      if(username==oldname){
        res.send({
          error:0,
          message:'success',
          data:'false'
        })
        return
      }
    }
    let sql = 'select count(1) `count` from `classes` where `username`=?'
    req.db.query(sql, { 
      replacements:[username],
      type: req.db.QueryTypes.SELECT
     }).then(result => {
      res.send({
        error: 0,
        message: 'success',
        data: result[0].count == 0 ? 'false' : 'true'
      })
    }).catch((error) => {
      console.log(error)
      res.send({
        error: 40,
        message: 'query fail'
      })
    })
  }

  // 班级信息的删除
  delete(req,res){
    let {id}=req.query
    let sql='delete from `classes` where `id`="'+id+'"'
    req.db.query(sql).then(()=>{
      res.send({
        error:0,
        message:'success'
      })
    }).catch(error=>{
      console.log(error)
      res.send({
        error:20,
        message:'delete fail'
      })
    })
  }


  // 编辑班级信息
  edit(req,res){
    let {classname,status,id}=req.body
    let sql='update `classes` set `classname`="'+classname+'",`status`="'+status+'" where `id`="'+id+'"'
    req.db.query(sql).then(()=>{
      res.send({
        error:0,
        message:'success'
      })
    }).catch(error=>{
      console.log(error)
      res.send({
        error:30,
        message:'update fail'
      })
    })
  }
}
module.exports = new Classes()